<!-- file wakil -->
<script> 
	
	$(function(){
		/* REMOVE DUPLICATE ID */
		var ids = [];
		$('*').each(function() {
		    if (this.id && this.id !== '') {
		        if (ids[this.id]) {
		            $(this).remove();
		        } else {
		            ids[this.id] = this
		        }
		    }
		});
		
		/* external function */
		
        function format(state) {
			tambahan = "";
			if (typeof $(state.element[0]).attr("alt") != "undefined")
				tambahan = $(state.element[0]).attr("alt");
            return state.text+tambahan ; 
        }
		
		$.fn.getCombobox = function(data, key, label, desc){
			var field = $(this);
			field.html("<option></option>");
			$.ajax({
				url : "c_public_function",
				data : data,
				type : 'POST', dataType : 'json'
			}).done(function(response){
				for(i=0;i<response.length;i++){
					var option = "<option value='"+response[i][key]+"' ";
					if (desc != "")
						option += " alt='<div class=desc>"+response[i][desc]+"</div>'";
						
					option += " >"+response[i][label]+"</option>";
					field.append(option);
				}
				
				field.select2({
					placeholder: "-",
					allowClear: true,
					formatResult: format,
					formatSelection: format,
					escapeMarkup: function (m) {
						return m;
					}
				});
			});
		}
		
		$.fn.serializeObject = function()
		{
		   var o = {};
		   var a = this.serializeArray();
		   $.each(a, function() {
			   if (o[this.name]) {
				   if (!o[this.name].push) {
					   o[this.name] = [o[this.name]];
				   }
				   o[this.name].push(this.value || '');
			   } else {
				   o[this.name] = this.value || '';
			   }
		   });
		   return o;
		};
		
		/* declarisasi */
		
		task_wakil = $("#task-wakil");
		grid_wakil = $("#grid-wakil");
		btnadd_wakil = $("#btn-add-wakil");
		btncancel_wakil =  $("#btn-cancel-wakil");
		btnsave_wakil = $("#btn-save-wakil");
		form_wakil = $("#form-wakil");
		portlet_wakil = $("#portlet-wakil");
		
		field_wakil_id = $("#wakil_id");
		field_m_mandor_id = $("#m_mandor_id");
		field_wakil_nama = $("#wakil_nama");
		field_wakil_telpon = $("#wakil_telpon");
		
		/* function */
		
		/* method started */
		
		/* make component */
		
		/* maybe you need * /
		
		field_task_assign_date.datepicker({ dateFormat : 'dd-MM-yy'});
		field_status.select2({
					placeholder: "-",
					allowClear: true,
					formatResult: format,
					formatSelection: format,
					escapeMarkup: function (m) {
						return m;
					}
				});
		m_staff_id.select2("val",dataRow.m_staff_id);
				
		/*                  */
		
		grid_wakil.unugrid({
			url: 'c_wakil',
			data: {task : 'LIST'},
			column :[
				{ label: 'm_mandor_id'  ,index: 'm_mandor_id'  },
				{ label: 'wakil_nama'  ,index: 'wakil_nama'  },
				{ label: 'wakil_telpon'  ,index: 'wakil_telpon'  },
				
				{ label: ''  ,index: 'wakil_id',type: 'action' }
			],
			deleteClick : function(id,dataRow, c ){
				if (id != null){
					confr = confirm("Apa Anda yakin menghapus data ini?");
					if (confr){
						var data = $(this).data();
						$.ajax({
							type: 'POST',
							url: 'c_wakil',
							data: {wakil_id: id, task : 'DELETE'},
							success: function(response){
								if(response==1){
									grid_wakil.unugrid('refresh');
								}
							}
						});
					}
				}
			},
			editClick : function(id,dataRow, c ){
				if (id != null){
					task_wakil.val("UPDATE");
					
					field_wakil_id.val(dataRow.wakil_id);
					field_m_mandor_id.val(dataRow.m_mandor_id);
					field_wakil_nama.val(dataRow.wakil_nama);
					field_wakil_telpon.val(dataRow.wakil_telpon);
					
					portlet_wakil.show();
				}
			}
		});
		
		/* event */
		
		btnadd_wakil.click(function(){
			btncancel_wakil.click();
			task_wakil.val("INSERT");
			portlet_wakil.show();
		});
		
		btncancel_wakil.click(function(){
			portlet_wakil.hide();
		});
		
		btnsave_wakil.click(function(){
			data = form_wakil.serializeObject();
			$.ajax({
				url : "c_wakil",
				data : data,
				type : 'POST'
			}).done(function(response){
				grid_wakil.unugrid('refresh');
				btncancel_wakil.click();
			});
		});
	});
</script>
	<div class="portlet box blue" hidden=hidden tabindex="-1" id="portlet-wakil">
	   <div class="portlet-title">
		  <div class="caption"><i class="icon-reorder"></i>Form wakil</div>
		  <div class="tools">
			 <a href="javascript:;" class="collapse"></a>
		  </div>
	   </div>

		<div class="portlet-body form">
		<!-- BEGIN FORM-->
		<form action="#" id="form-wakil" class="horizontal-form" onSubmit="javascript:return false;">

		<input type="hidden" value="INSERT" name="task" id="task-wakil">
		<input type="hidden"  id="wakil_id" name="wakil_id" />
		
		<input id="task_id" name="task_id" hidden=hidden  />

			 <div class="form-body">
				<h3 class="form-section">wakil</h3>

				<div class="row"><div class="col-md-6"><div class="form-group"><label class="control-label">m_mandor_id:</label><input id="m_mandor_id" name="m_mandor_id" class="form-control" /></div></div></div>
				<div class="row"><div class="col-md-6"><div class="form-group"><label class="control-label">wakil_nama:</label><input id="wakil_nama" name="wakil_nama" class="form-control" /></div></div></div>
				<div class="row"><div class="col-md-6"><div class="form-group"><label class="control-label">wakil_telpon:</label><input id="wakil_telpon" name="wakil_telpon" class="form-control" /></div></div></div>
		
			 </div>
			 <div class="form-actions right">
				<button type="reset" id="btn-cancel-wakil" class="btn default">Cancel</button>
				<button type="submit" id="btn-save-wakil" class="btn blue"><i class="icon-ok"></i> Save</button>
			 </div>
		  </form>
		  <!-- END FORM--> 
	   </div>
	</div>
    
	 <div class="row">
		<div class="col-md-12">
		   <!-- BEGIN SAMPLE TABLE PORTLET-->
		   <div class="portlet box yellow">
			  <div class="portlet-title">
				 <div class="caption"><i class="icon-cogs"></i>wakil</div>
				 <div class="tools">
					<a href="javascript:;" class="collapse"></a>
				 </div>
			  </div>
			  <div class="portlet-body flip-scroll">
			  
			<button id="btn-add-wakil" class="btn btn-primary">Add</button>
			<p>&nbsp;</p>
			
			  <div id="grid-wakil" class="table-responsive"></div>
				</div>
			 </div>
		</div>
	</div>